#include "window_setting.h"
#include "ui_window_setting.h"
#include "common_fun.h"

int machine_driver_array_limit = 2;
QString machine_driver_array[100] = {
   "Gprinter GP-2120T",
   "Gprinter GP-3120TL"
};

int code_type_array_limit = 3;
QString code_type_array[100] = {
    "txt",
    "BARcode",
    "QRcode"
};
window_setting::window_setting(QWidget *parent,MACHINE_SETTING* pointer):
    QDialog(parent),
    ui(new Ui::window_setting)
{
    ui->setupUi(this);
    SETTING = pointer;  //获得父窗口保存的对象的指针。

    ui->LOCK->setChecked(true);
    Locks_change();
    Loadsetting();

    connect_slots();
    block();
}

window_setting::~window_setting()
{
    delete ui;
}
void window_setting::closeEvent(QCloseEvent *event)
{
    emit setting_close_window();
}

void window_setting::Locks_change()
{
    if(ui->LOCK->isChecked())
    {
        ui->LABEL_NORMAL_H_INPUT->setEnabled(false);
        ui->LABEL_NORMAL_W_INPUT->setEnabled(false);
        ui->LABEL_NORMAL_OFF_H_INPUT->setEnabled(false);
        ui->LABEL_NORMAL_OFF_W_INPUT->setEnabled(false);
        ui->Code_Type_select->setEnabled(false);
        ui->Machine_Driver_select->setEnabled(false);
        ui->LABEL_NORMAL_L_DISTANCE_INPUT->setEnabled(false);
    }
    else
    {
        ui->LABEL_NORMAL_H_INPUT->setEnabled(true);
        ui->LABEL_NORMAL_W_INPUT->setEnabled(true);
        ui->LABEL_NORMAL_OFF_H_INPUT->setEnabled(true);
        ui->LABEL_NORMAL_OFF_W_INPUT->setEnabled(true);
        ui->Code_Type_select->setEnabled(true);
        ui->Machine_Driver_select->setEnabled(true);
        ui->LABEL_NORMAL_L_DISTANCE_INPUT->setEnabled(true);
    }
}
void window_setting::Loadsetting(){
    SETTING->read_setting();
    //一般设置
    ui->LABEL_NORMAL_W_INPUT->setText(int_2_qstring(SETTING->get_NORMAL().label_width));
    ui->LABEL_NORMAL_H_INPUT->setText(int_2_qstring(SETTING->get_NORMAL().label_height));
    ui->LABEL_NORMAL_OFF_W_INPUT->setText(int_2_qstring(SETTING->get_NORMAL().label_offset_width));
    ui->LABEL_NORMAL_OFF_H_INPUT->setText(int_2_qstring(SETTING->get_NORMAL().label_offset_height));
    ui->LABEL_NORMAL_L_DISTANCE_INPUT->setText(int_2_qstring(SETTING->get_NORMAL().line_distance));
        //单独设置2个下拉选框
        Set_driver();
        Set_type();
    //一维码
    ui->LABEL_BARCODE_A_INPUT->setText(int_2_qstring(SETTING->get_BARCODE().barcode_A));
    ui->LABEL_BARCODE_B_INPUT->setText(int_2_qstring(SETTING->get_BARCODE().barcode_B));
    ui->LABEL_BARCODE_TURN_INPUT->setText(int_2_qstring(SETTING->get_BARCODE().barcode_turn));
    ui->LABEL_BARCODE_TYPE_INPUT->setText(SETTING->get_BARCODE().barcode_type);
    ui->LABEL_BARCODE_HEIGHT_INPUT->setText(int_2_qstring(SETTING->get_BARCODE().barcode_height));
    ui->LABEL_BARCODE_HUMAN_INPUT->setText(int_2_qstring(SETTING->get_BARCODE().barcode_human));
    //二维码
    ui->LABEL_QRCODE_CELL_INPUT->setText(int_2_qstring(SETTING->get_QRCODE().qrcode_cell));
    ui->LABEL_QRCODE_ECC_INPUT->setText(SETTING->get_QRCODE().qrcode_ecc);
    ui->LABEL_QRCODE_MODE_INPUT->setText(SETTING->get_QRCODE().qrcode_mode);
    ui->LABEL_QRCODE_MASK_INPUT->setText(int_2_qstring(SETTING->get_QRCODE().qrcode_mask));
    ui->LABEL_QRCODE_TURN_INPUT->setText(int_2_qstring(SETTING->get_QRCODE().qrcode_turn));
    ui->LABEL_QRCODE_MODEL_INPUT->setText(int_2_qstring(SETTING->get_QRCODE().qrcode_model));
    //文本
    ui->LABEL_TEXT_HORIZONTAL_INPUT->setText(int_2_qstring(SETTING->get_TEXT().text_horizontal));
    ui->LABEL_TEXT_TURN_INPUT->setText(int_2_qstring(SETTING->get_TEXT().text_turn));
    ui->LABEL_TEXT_TYPE_INPUT->setText(SETTING->get_TEXT().text_type);
    ui->LABEL_TEXT_VERTICAL_INPUT->setText(int_2_qstring(SETTING->get_TEXT().text_vertical));

    MESSAGE_BOX("加载现有配置成功");
}
void window_setting::Savesetting(){

    //一般设置
    SETTING->get_NORMAL().label_height = qstring_2_int(ui->LABEL_NORMAL_H_INPUT->text());
    SETTING->get_NORMAL().label_width = qstring_2_int(ui->LABEL_NORMAL_W_INPUT->text());
    SETTING->get_NORMAL().label_offset_height = qstring_2_int(ui->LABEL_NORMAL_OFF_H_INPUT->text());
    SETTING->get_NORMAL().label_offset_width = qstring_2_int(ui->LABEL_NORMAL_OFF_W_INPUT->text());
    SETTING->get_NORMAL().Machine_driver = ui->Machine_Driver_select->itemText(ui->Machine_Driver_select->currentIndex());
    SETTING->get_NORMAL().Code_type = ui->Code_Type_select->itemText(ui->Code_Type_select->currentIndex());
    SETTING->get_NORMAL().line_distance = qstring_2_int(ui->LABEL_NORMAL_L_DISTANCE_INPUT->text());
    //一维码
    SETTING->get_BARCODE().barcode_A = qstring_2_int(ui->LABEL_BARCODE_A_INPUT->text());
    SETTING->get_BARCODE().barcode_B = qstring_2_int(ui->LABEL_BARCODE_B_INPUT->text());
    SETTING->get_BARCODE().barcode_height = qstring_2_int(ui->LABEL_BARCODE_HEIGHT_INPUT->text());
    SETTING->get_BARCODE().barcode_human = qstring_2_int(ui->LABEL_BARCODE_HUMAN_INPUT->text());
    SETTING->get_BARCODE().barcode_turn = qstring_2_int(ui->LABEL_BARCODE_TURN_INPUT->text());
    SETTING->get_BARCODE().barcode_type = ui->LABEL_BARCODE_TYPE_INPUT->text();
    //二维码
    SETTING->get_QRCODE().qrcode_cell = qstring_2_int(ui->LABEL_QRCODE_CELL_INPUT->text());
    SETTING->get_QRCODE().qrcode_ecc = ui->LABEL_QRCODE_ECC_INPUT->text();
    SETTING->get_QRCODE().qrcode_mode = ui->LABEL_QRCODE_MODE_INPUT->text();
    SETTING->get_QRCODE().qrcode_mask = qstring_2_int(ui->LABEL_QRCODE_MASK_INPUT->text());
    SETTING->get_QRCODE().qrcode_model = qstring_2_int(ui->LABEL_QRCODE_MODEL_INPUT->text());
    SETTING->get_QRCODE().qrcode_turn = qstring_2_int(ui->LABEL_QRCODE_TURN_INPUT->text());
    //文本
    SETTING->get_TEXT().text_type = ui->LABEL_TEXT_TYPE_INPUT->text();
    SETTING->get_TEXT().text_horizontal = qstring_2_int(ui->LABEL_TEXT_HORIZONTAL_INPUT->text());
    SETTING->get_TEXT().text_vertical = qstring_2_int(ui->LABEL_TEXT_VERTICAL_INPUT->text());
    SETTING->get_TEXT().text_turn = qstring_2_int(ui->LABEL_TEXT_TURN_INPUT->text());

    SETTING->save_setting();
    MESSAGE_BOX("保存现有配置成功");

}
void window_setting::connect_slots()
{
    connect(ui->LOCK,SIGNAL(clicked()),this,SLOT(Locks_change()));
    connect(ui->SAVE_SETTING,SIGNAL(clicked()),this,SLOT(Savesetting()));
    connect(ui->LOAD_SETTING,SIGNAL(clicked()),this,SLOT(Loadsetting()));
    connect(ui->RESET_PARAM,SIGNAL(clicked()),this,SLOT(Reset_param()));
}

void window_setting::MESSAGE_BOX(QString str)
{
    QMessageBox box;    //调用box对象的information方法，进行提示。
    box.information(this,"提示信息",str,"OK");
}
void window_setting::block()
{
    //一维码
    ui->LABEL_BARCODE_A_INPUT->setEnabled(false);
    ui->LABEL_BARCODE_B_INPUT->setEnabled(false);
    ui->LABEL_BARCODE_TURN_INPUT ->setEnabled(false);
    ui->LABEL_BARCODE_TYPE_INPUT->setEnabled(false);
    ui->LABEL_BARCODE_HEIGHT_INPUT->setEnabled(false);
    ui->LABEL_BARCODE_HUMAN_INPUT->setEnabled(false);
    //二维码
    ui->LABEL_QRCODE_CELL_INPUT->setEnabled(false);
    ui->LABEL_QRCODE_ECC_INPUT->setEnabled(false);
    ui->LABEL_QRCODE_MODE_INPUT->setEnabled(false);
    ui->LABEL_QRCODE_MASK_INPUT->setEnabled(false);
    ui->LABEL_QRCODE_TURN_INPUT->setEnabled(false);
    ui->LABEL_QRCODE_MODEL_INPUT->setEnabled(false);
    //文本
    ui->LABEL_TEXT_HORIZONTAL_INPUT->setEnabled(false);
    ui->LABEL_TEXT_TURN_INPUT->setEnabled(false);
    ui->LABEL_TEXT_TYPE_INPUT->setEnabled(false);
    ui->LABEL_TEXT_VERTICAL_INPUT->setEnabled(false);
}

void window_setting::Set_driver()   //改变setting_normal中的驱动名称
{
    QString target = SETTING->get_NORMAL().Machine_driver;;

    int i;
    for(i=0;i<machine_driver_array_limit;i++)
    {
        if(target == machine_driver_array[i])
        {
            ui->Machine_Driver_select->setCurrentIndex(i);
            //找到了参数就直接返回
            return;
        }
    }

    //如果没找到，就默认为"Gprinter GP-2120T"
    ui->Machine_Driver_select->setCurrentIndex(0);
}
void window_setting::Set_type()     //改变setting_normal中的条码类型
{
    QString target = SETTING->get_NORMAL().Code_type;

    int i;
    for(i=0;i<code_type_array_limit;i++)
    {
        if(target == code_type_array[i])
        {
            ui->Code_Type_select->setCurrentIndex(i);
            //找到了参数就直接返回
            return;
        }
    }

    //如果没找到，就默认为纯文本打印
    ui->Machine_Driver_select->setCurrentIndex(0);
}
void window_setting::Reset_param()
{
    //把所有参数还原到最初始的状态。
    MESSAGE_BOX("参数重置成功");
    SETTING->init_setting();
    Loadsetting();
}

